package com.bianguo.android.functiondemo.retrofit;

import android.net.ParseException;

import com.bianguo.android.functiondemo.utils.MLog;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.MalformedJsonException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.exceptions.CompositeException;
import retrofit2.HttpException;

/**
 * Created by Administrator on 2018/5/3.
 */

public class FactoryException {

    private static final String HttpException_MSG = "网络异常";
    private static final String ConnectException_MSG = "服务器连接错误";
    private static final String JSONException_MSG = "数据格式错误";
    private static final String UnknownHostException_MSG = "无法解析该域名";
    private static final String UNKNOWN_ERROR = "未知错误";

    /**
     * 解析异常
     *
     * @param e
     * @return
     */
    public static ApiException analysisException(Throwable e) {
        MLog.mLog("http error : " + e.getMessage() + ", clsName = " + e.getClass().getName());
        ApiException apiException = new ApiException(e);
        if (e instanceof HttpException) {
             /*网络异常*/
            apiException.setCode(CodeException.HTTP_ERROR);
            apiException.setMessage(HttpException_MSG);
        } else if (e instanceof HttpTimeException) {
             /*自定义运行时异常*/
            HttpTimeException exception = (HttpTimeException) e;
            apiException.setCode(CodeException.RUNTIME_ERROR);
            apiException.setMessage(exception.getMessage());
        } else if (e instanceof ConnectException || e instanceof SocketTimeoutException) {
             /*链接异常*/
            apiException.setCode(CodeException.HTTP_ERROR);
            apiException.setMessage(ConnectException_MSG);
        } else if (e instanceof JsonSyntaxException || e instanceof JSONException || e instanceof ParseException) {
             /*解析异常*/
            apiException.setCode(CodeException.JSON_ERROR);
            apiException.setMessage(JSONException_MSG);
        } else if (e instanceof UnknownHostException) {
            /*无法解析该域名异常*/
            apiException.setCode(CodeException.UNKOWNHOST_ERROR);
            apiException.setMessage(UnknownHostException_MSG);
        } else if (e instanceof CompositeException) {
            CompositeException compositeE = (CompositeException) e;
            for (Throwable throwable : compositeE.getExceptions()) {
                if (throwable instanceof SocketTimeoutException || e instanceof ConnectException) {
                    apiException.setCode(CodeException.HTTP_ERROR);
                    apiException.setMessage(ConnectException_MSG);
                } else if (throwable instanceof UnknownHostException) {
                    apiException.setCode(CodeException.UNKOWNHOST_ERROR);
                    apiException.setMessage(UnknownHostException_MSG);
                } /*else if (throwable instanceof RxCacheException) {
                    apiException.setCode(CodeException.CACHE_ERROR);
                    apiException.setMessage("缓存文件已过期,请连接网络后再尝试");
                }*/ else if (throwable instanceof MalformedJsonException) {
                    apiException.setCode(CodeException.JSON_ERROR);
                    apiException.setMessage(JSONException_MSG);
                }
            }
        } else {
            /*未知异常*/
            apiException.setCode(CodeException.UNKNOWN_ERROR);
            apiException.setMessage(UNKNOWN_ERROR);
        }
        return apiException;
    }
}
